package com.equis.architecture.controls {

	import mx.containers.Canvas;
	import mx.containers.TitleWindow;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.managers.CursorManager;
	
	public class WindowLock {
	
	    public static const INSTANCE:WindowLock = new WindowLock();
	
	    private var canvas:Canvas;
	    private var locked:Boolean = false;
	
	    public function WindowLock() {
	        canvas = buildCanvas();
	    }
	    
	    private function buildCanvas():Canvas {
	        var canvas:Canvas = new Canvas();
	        canvas.percentHeight = 100;
	        canvas.percentWidth = 100;
	        canvas.setStyle("backgroundColor", 0);
	        canvas.alpha = 0.001;
	        return canvas;
	    }
	
	
	    public function lock(popUp:ILockable = null):void {
	        if(locked) {
	            return;
	        }
	        locked = true;
	        CursorManager.setBusyCursor();
	
			if(popUp != null) {
				popUp.lock();
			} else {
				FlexGlobals.topLevelApplication.addElement(canvas);
				FlexGlobals.topLevelApplication.setElementIndex(canvas, FlexGlobals.topLevelApplication.numElements - 1);
			}
	
	    }
	
	
	    public function unLock():void {
	    	if(locked) {
				if(canvas.parent is ILockable) {
			        ILockable(canvas.parent).unlock();
				} else {
					FlexGlobals.topLevelApplication.removeElement(canvas);
				}
		        CursorManager.removeBusyCursor();
		        locked = false;
	    	}
	    }
	}
}
